package com.qf.listen;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @author H
 * @create 2020-10-30 15:28
 * @strive To be whoever you want to be.
 * @Description:
 */
@Component
public class MailListen {
    @Autowired
    JavaMailSender javaMailSender;


    @Autowired
    RedisTemplate redisTemplate;
   /* @Autowired
    TemplateEngine templateEngine;*/

    //邮箱发送
    @RabbitListener(queues = "mail-queue")
    public void sendMail(Map map) {
        String from = (String) map.get("from");
        String mail = (String) map.get("mail");
        String code = (String) map.get("code");

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(from);

        simpleMailMessage.setTo(mail);
        simpleMailMessage.setSubject("登录验证码");
        simpleMailMessage.setText(code);
        javaMailSender.send(simpleMailMessage);
        //使用redis存储 验证码

        redisTemplate.opsForValue().set(mail, code);
        redisTemplate.expire(mail, 5, TimeUnit.MINUTES);

    }

   /* @RabbitListener(queues = "mail-queue")
    public void sendMail(Map map) {
        String from = (String) map.get("from");
        Integer id = (Integer) map.get("id");
        String mail = (String) map.get("mail");
        //向模板页面放置id
        Context context = new Context();
        context.setVariable("id", id);
        String active = templateEngine.process("active", context);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(from);
            mimeMessageHelper.setTo(mail);
            mimeMessageHelper.setSubject("激活邮件");
            mimeMessageHelper.setText(active, true);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }*/

}
